import XEUtils from 'xe-utils'
export const adapterForms = (formData, formConfigData) => {
  if (!formData) return;
  let formRules = {};
  let formListData = XEUtils.clone(formData, true);
  formListData.forEach((formItem, i) => {
    let formConfigObj = formConfigData ?
      formConfigData[formItem.fromSource] || formConfigData[formItem.code] : {};
    let publicConfigObj = formConfigData ? formConfigData["publicAttr"] : {};
    let formConfig = XEUtils.isString(formItem.config) ? XEUtils.toStringJSON(formItem.config) : formItem.config || {}
    formItem.configDefault = XEUtils.merge(
      formItem.configDefault,
      publicConfigObj,
      formConfig,
      formConfigObj
    );

    formRules[formItem.code] = [];
    let formItemVaild = formItem.configDefault.validator ? formItem.configDefault.validator : formItem.validator ? formItem.validator : null;
    if (formItemVaild) {
      // console.log(formItem.code, formItem.configDefault)
      formRules[formItem.code] = [{
        required: formItem.isRequired ? true : false,
        trigger: formItem.configDefault.triggerRules ? formItem.configDefault.triggerRules : "blur",
        validator: formItemVaild
      }]
    } else {
      if (formItem.isRequired) {
        formRules[formItem.code] = [{
          required: true,
          trigger: formItem.configDefault.triggerRules ? formItem.configDefault.triggerRules : "blur",
          message: formItem.configDefault.placeholderLabel ? formItem.configDefault.placeholderLabel : formItem.title ? `请填写${formItem.title}` : '必填项！'
        }]
      }
    }
  })
  return {
    formListData,
    formRules
  }
}
